﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace HdhWpf.AppCode
{
    public class HdhViewModel : INotifyPropertyChanged
    {
        private string _username;
        private string _password;
        private string _statusMessage;
        private Brush _statusColor;
        private bool _showStatus;

        public string UserName
        {
            get => _username;
            set
            {
                _username = value;
                OnPropertyChanged();
            }
        }

        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                OnPropertyChanged();
            }
        }

        public string StatusMessage
        {
            get => _statusMessage;
            set
            {
                _statusMessage = value;
                OnPropertyChanged();
            }
        }

        public Brush StatusColor
        {
            get => _statusColor;
            set
            {
                _statusColor = value;
                OnPropertyChanged();
            }
        }

        public bool ShowStatus
        {
            get => _showStatus;
            set
            {
                _showStatus = value;
                OnPropertyChanged();
            }
        }

        public ICommand LoginCommand { get; }
        public ICommand RegisterCommand { get; }

        public HdhViewModel()
        {
            LoginCommand = new RelayCommand(ExecuteLogin, CanExecuteLogin);
            RegisterCommand = new RelayCommand(ExecuteRegister);
            UserName = "onkey";
            Password = "123456";
            // 初始化状态
            StatusColor = Brushes.Red;
            ShowStatus = false;
        }

        private bool CanExecuteLogin(object parameter)
        {
            return !string.IsNullOrWhiteSpace(UserName) && !string.IsNullOrWhiteSpace(Password);
        }

        private void ExecuteLogin(object parameter)
        {
            // 模拟登录验证
            if (UserName == "admin" && Password == "123456")
            {
                StatusMessage = "登录成功！";
                StatusColor = Brushes.Green;
                ShowStatus = true;

                // 在实际应用中，这里会跳转到主窗口
                MessageBox.Show($"欢迎 {UserName} 登录系统！", "登录成功", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                StatusMessage = "用户名或密码错误！";
                StatusColor = Brushes.Red;
                ShowStatus = true;
                MessageBox.Show($"{StatusMessage}", "登录失败", MessageBoxButton.OK, MessageBoxImage.Error);
                UserName = "";
                Password = "";
            }
        }

        private void ExecuteRegister(object parameter)
        {
            MessageBox.Show("跳转到注册页面...", "注册",
                MessageBoxButton.OK, MessageBoxImage.Information);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
